package commandes;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import bdd.DBS;
import beans.Client;
import beans.Logement;

public class CommandeMajLogement implements Commande {
	  private String next;

	  public CommandeMajLogement(String next) {
	        this.next = next;
	  }

	public String execute(HttpServletRequest req) throws Exception {
		HttpSession session = req.getSession(true);
		if(session.getAttribute("droit").equals("administrateur")) {
			DBS db = DBS.getInstance();
			
			if(req.getParameter("suppr")!=null){
				db.getDB_Logement().deleteLogement(Integer.parseInt(req.getParameter("suppr")));
			}
			
			if(req.getParameter("mod")!=null){
				req.setAttribute("mod",req.getParameter("mod"));
			}else{
				req.setAttribute("mod",0);
			}
			
			String quartier = req.getParameter("quartier");
			String adresse = req.getParameter("adresse");
			String type = req.getParameter("type");
			String loyer = req.getParameter("loyer");
			String idCli = req.getParameter("idCli");
			String idProp = req.getParameter("idProp");
			
			if(quartier != null && adresse !=null && type != null && loyer!=null){
				Logement l=new Logement(Integer.parseInt(idCli),Integer.parseInt(idProp), quartier, adresse, type, Double.parseDouble(loyer));
				db.getDB_Logement().updateLogement(l);
			}else if(quartier == null || adresse == null || type == null || loyer!=null){
				req.setAttribute("erreur", true);
			}
			
			List<Logement> logs = db.getDB_Logement().getLogements();
			req.setAttribute("logements", logs);
			return next;
		} else {
			return "index.jsp";
		}
	}

}